package com.welsy.authoring.business
{
	import com.welsy.authoring.model.CourseModelLocator;
	import com.welsy.authoring.view.coursecomponents.ElementIconView;
	import com.welsy.authoring.view.coursecomponents.ModuleIconView;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.net.*;
	
	import mx.containers.*;
	import mx.controls.Alert;
	import mx.core.Container;
	import mx.core.DragSource;
	import mx.core.IUIComponent;
	import mx.events.*;
	import mx.managers.DragManager;
	import mx.rpc.soap.*;

	public class DragAndDropUtils
	{
		
		public var cestinoPanel:Object;
		
		private var sovrapposizione:int = 0;  
		
		public function setCestinoPanel(myVBox:VBox):void {
        	cestinoPanel = Object(myVBox);
        }        
        
        //non valida il drag per gli elementi test valutativi dei corsi pubblicati
        private function validateDrag(dragInitiator:IUIComponent):Boolean {
        	var model:CourseModelLocator = CourseModelLocator.getInstance()
        	var returnValue:Boolean
        	        	
        	if (model.selectedCourse.course.enableItemTest() == false) {        	
        		switch (dragInitiator.name) {
        			case "elemento":
        				var sourceObj:Object = Object(dragInitiator);	            	
        				var myType:String = String(ElementType.elementCode[sourceObj.id]);
        				if (myType == "0")
        					returnValue = false
        				else
        					returnValue = true
        				break
        			case "elemento2":
        				var el:ElementIconView = ElementIconView(dragInitiator)
        				if (el.type == "0")
        					returnValue = false
        				else
        					returnValue = true
        				break
        			default:
        				returnValue = true
        				break;
        		}
        		return returnValue
        	}
        	else
        		return true
        }   
                 
        public function dragIt(event:MouseEvent):void {       	
        	var dragInitiator:IUIComponent = IUIComponent(event.currentTarget);
            var ds:DragSource = new DragSource();
            
            if (validateDrag(dragInitiator))         
            	DragManager.doDrag(dragInitiator, ds, event,null,0.0,0.0,0.5,true);
        }

        public function doDragEnter(event:DragEvent):void {
            var dropTarget:IUIComponent = IUIComponent(event.currentTarget);
            if ((((event.dragInitiator.name == "modulo") && (event.currentTarget.name == "area"))
            ||  ((event.dragInitiator.name == "elemento") && (event.currentTarget.name == "modulo2"))
            ||  ((event.dragInitiator.name == "audio") && (event.currentTarget.name == "elemento2") && (ElementIconView(event.currentTarget).type != "0") && (ElementIconView(event.currentTarget).type != "1"))
            ||  ((event.dragInitiator.name == "modulo2") && (event.currentTarget.name == "cestino"))
            ||  ((event.dragInitiator.name == "modulo2") && (event.currentTarget.name == "modulo2"))
            ||  ((event.dragInitiator.name == "elemento2") && (event.currentTarget.name == "cestino"))
            ||  ((event.dragInitiator.name == "elemento2") && (event.currentTarget.name == "elemento2"))
            ||  ((event.dragInitiator.name == "audio2") && (event.currentTarget.name == "cestino"))
            ||  ((event.dragInitiator.name == "elemento2") && (event.currentTarget.name == "modulo2"))
            ||  ((event.dragInitiator.name == "modulo2") && (event.currentTarget.name == "area"))
            )){
	            if(event.dragInitiator == event.currentTarget)
		        	sovrapposizione = 1;
		        else
		        	sovrapposizione = 0;
		        		             
				DragManager.acceptDragDrop(dropTarget);
            }
        }
                      
		public function doDragDrop(event:DragEvent):void {
			
			if(sovrapposizione == 0){	
	            if (event.dragInitiator.name == "modulo"){
	            	var module:ModuleIconView = ElementFactory.getModule ();
		            module.addEventListener(DragEvent.DRAG_ENTER,doDragEnter);
		            module.addEventListener(DragEvent.DRAG_DROP,doDragDrop);
		            module.addEventListener(MouseEvent.MOUSE_MOVE, dragIt);
		           	event.currentTarget.addChild(module);
		           	
		           	//modulehitarea
		           	var modulehitarea:Canvas = new Canvas;
		           	modulehitarea.name = "modulehitarea";
		           	modulehitarea.width = 40;
		           	modulehitarea.height = 30;
		           	modulehitarea.setStyle('backgroundImage', 'assets/moduleGrip.gif');
		           	module.hitArea = modulehitarea;
					module.mouseEnabled = false;
						
					//todo improve courseDao management					
					var courseDaoObj:CourseDao = event.currentTarget.parent.parent.courseDao;
					modulehitarea.addEventListener(MouseEvent.CLICK, courseDaoObj.moduleProperties);
		            module.addChild(modulehitarea);
		        }
	        
		        if (event.dragInitiator.name == "modulo2"){
		           	if (event.currentTarget.name == "cestino") {
		           		var widthModule:Number = event.dragInitiator.width;
		           		var heightModule:Number = event.dragInitiator.height;
		           		var alphaModule:Number = event.dragInitiator.alpha;		           			
		        			cestinoPanel.addChild(event.dragInitiator);
		        			event.dragInitiator.scaleX = 0.5;
		        			event.dragInitiator.scaleY = 0.5;			        			
	        		}else{
	        			if (Container(event.dragInitiator.parent).id == "cestinoPanel") {
		        			event.dragInitiator.scaleX = 1;
		        			event.dragInitiator.scaleY = 1;
		        			//move module from the bin to the stage
	        				event.currentTarget.addChildAt(event.dragInitiator,0);
		        		}		        		
	        		}	        		
		        }
		        
		        if (event.dragInitiator.name == "modulo2"){			        		        	
		           	if (event.currentTarget.name == "modulo2")
		           	{
		           		var i:int = event.currentTarget.parent.getChildIndex(event.currentTarget);
		           		var pt:Point = new Point(event.localX, event.localY);
	          			pt = event.currentTarget.localToGlobal(pt);
		           		pt = event.currentTarget.globalToContent(pt);
		           		var posizioneMouse: int = pt.y;
		           		var k:int = 0;
		           		
		           		if ( posizioneMouse < event.currentTarget.height/2){
		           			k = 0;
		           		}
		           		if ( posizioneMouse >= event.currentTarget.height/2){
		           			k = 1;
		           		}
		           		if (i < event.currentTarget.parent.numChildren-1) {
		           			//modificata condizione andava in out of bound
		            		event.currentTarget.parent.addChildAt(event.dragInitiator,i+k);
		            	}	 
	        		}
	        	}
		            
	            if (event.dragInitiator.name =="elemento") {
	            	var element:ElementIconView = ElementFactory.getElementByEvent(event);
		            element.type = ElementType.elementCode[element.id];
		           
		            element.addEventListener(DragEvent.DRAG_ENTER,doDragEnter);
		            element.addEventListener(DragEvent.DRAG_DROP,doDragDrop);
		            element.addEventListener(MouseEvent.MOUSE_MOVE, dragIt);
		            
		            //new
		            var myCourseDao:CourseDao = event.currentTarget.parent.parent.parent.courseDao;
		            if ((element.type == "0") || (element.type  == "1"))
		            	element.addEventListener(MouseEvent.CLICK, myCourseDao.testProperties);
		            else
		            	element.addEventListener(MouseEvent.CLICK, myCourseDao.elementProperties);
		            
		            event.currentTarget.addChild(element);
		        }
		        
	            if (event.dragInitiator.name == "elemento2"){
	            	if (event.currentTarget.name == "cestino") {
	           				cestinoPanel.addChild(event.dragInitiator);	           					        				        			
	        		}
		        }
		        
		        if (event.dragInitiator.name == "elemento2"){
	            	if (event.currentTarget.name == "modulo2"){
	            		event.currentTarget.addChildAt(event.dragInitiator,1);
	                }
		        }
		        
		        if (event.dragInitiator.name == "elemento2"){
		           	if (event.currentTarget.name == "elemento2"){         		
		           		var j:int = event.currentTarget.parent.getChildIndex(event.currentTarget);
		           		var pt2:Point = new Point(event.localX, event.localY);
	          			pt2 = event.currentTarget.localToGlobal(pt2);
		           		pt2 = event.currentTarget.globalToContent(pt2);
		           		var posizioneMouse2: int = pt2.x;
		           		var w:int = 0;
		           		
		           		if ( posizioneMouse2 < event.currentTarget.width/2){
		           			w = 0;
		           		}
		           		
		           		if ( posizioneMouse2 >= event.currentTarget.width/2){
		           			w = 1
		           		}
		           		
		           		if (j <= event.currentTarget.parent.numChildren-1) {
		            		event.currentTarget.parent.addChildAt(event.dragInitiator,j+w);
		            	}
		            }
		        }	 
		        
		        if (event.dragInitiator.name == "audio"){
		        	var elementTarget:ElementIconView = ElementIconView(event.currentTarget);
		        	if ((elementTarget.getChildren().length == 0) && (elementTarget.type != "0") && (elementTarget.type != "1")){
	            		var audio:ElementIconView = ElementFactory.getAudioElement();
		            	audio.addEventListener(MouseEvent.MOUSE_MOVE, dragIt);
		            	elementTarget.addChild(audio);
		         	}		            
		        }
		        
	        	if (event.dragInitiator.name == "audio2") {
	        		if (event.currentTarget.name == "cestino") {
	        				cestinoPanel.addChild(event.dragInitiator);
	        		}else{
	        			if (Container(event.dragInitiator.parent).id == "cestinoPanel") {
		        			event.dragInitiator.scaleX = 1;
		        			event.dragInitiator.scaleY = 1;}
	        			event.currentTarget.addChild(event.dragInitiator);
	        		}
            	}
         	}
         	else {
         		Alert.show("Non è possibile inserire un alemento all'interno di se stesso");
			}
	    }     		
	}
}